package protocol.transactions;

import java.lang.reflect.Type;
import java.util.List;

import protocol.XoneProtocol;
import protocol.XoneServiceCode;
import protocol.meta.UserDynamic;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.netease.http.THttpRequest;

public class GetDynamicListTransaction extends XoneBaseTransaction {

    private String[] mIds;

    public GetDynamicListTransaction(String[] ids) {
        super(TRANSACTION_TYPE_GET_DYNAMIC_LIST);
        mIds = ids;
    }

    @Override
    public void onTransact() {
        THttpRequest request = XoneProtocol.getInstance().createGetFriendDynamicListRequest(mIds);
        sendRequest(request);
    }

    @Override
    protected void onXoneTransactionSuccess(int code, Object obj) {
        if (obj != null && obj instanceof JsonElement) {
            JsonElement dynamicListJson = ((JsonElement)obj).getAsJsonObject().get("list");
            List<UserDynamic> dynamicList;
            Gson gson = new Gson();
            Type collectionType = new TypeToken<List<UserDynamic>>(){}.getType();
            dynamicList = gson.fromJson(dynamicListJson, collectionType);
            notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, dynamicList);
        } else {
            // TODO
            // notifyError(errCode, arg3)
        }
    }

}
